package com.smit.adapter;

import android.content.Context;

import com.smit.icast.ICastDevice;
import com.smit.icastplus.ICastPlusDevice;
import com.smit.utils.LogUtil;

import java.util.List;
/**
 * DVB设备管理
 */
public class DVBDeviceManager {
    /** 设备类型未知 */
    public static final int DVB_DEVICE_NONE = 0;
    /** icast 设备 */
    public static final int DVB_DEVICE_ICAST = 1;
    /** icastplus 设备 */
    public static final int DVB_DEVICE_ICAST_PLUS = 2;

    private static final String TAG = "DVBDeviceManager";

    private DVBDevice dvbDevice;
    private ICastDevice icastDevice;
    private ICastPlusDevice icastPlusDevice;
    private int dvbDeviceType; //0 no device; 1 icast; 2 icastplus
    private Context context;

    /**
     * 构造函数
     * @param context Android application context
     */
    public DVBDeviceManager(Context context) {
        this.context = context;
        icastDevice = new ICastDevice(context);
        icastPlusDevice = new ICastPlusDevice(context);
        dvbDevice = icastDevice;
        dvbDeviceType = DVB_DEVICE_ICAST;
        initialize();
    }

    /**
     * 初始化
     */
    private void initialize() {
        detectDevice();
    }

    /**
     * 获取设备
     * @return DVBDevice对象
     */
    public DVBDevice getDVBDevice() {
        return dvbDevice;
    }

    /**
     * 获取设备类型
     * @return 设备类型
     * @see #DVB_DEVICE_NONE
     * @see #DVB_DEVICE_ICAST
     * @see #DVB_DEVICE_ICAST_PLUS
     */
    public int getDeviceType() {
        LogUtil.info(TAG, "getDVBDeviceType start>>>");
        LogUtil.info(TAG, "getDVBDeviceType dvbDeviceType=" + dvbDeviceType);
        dvbDeviceType = DVB_DEVICE_NONE;
        if (icastDevice.isConnected()) {
            dvbDeviceType = DVB_DEVICE_ICAST;
            dvbDevice = icastDevice;
        } else if (icastPlusDevice.isConnected()) {
            dvbDeviceType = DVB_DEVICE_ICAST_PLUS;
            dvbDevice = icastPlusDevice;
        } else {
            dvbDeviceType = DVB_DEVICE_ICAST;
            dvbDevice = icastDevice;
        }
        LogUtil.info(TAG, "getDVBDeviceType end<<<");
        return dvbDeviceType;
    }
    /**
     * 设置设备类型
     * @param deviceType  设备类型
     * @see #DVB_DEVICE_NONE
     * @see #DVB_DEVICE_ICAST
     * @see #DVB_DEVICE_ICAST_PLUS
     */
    public void setDeviceType(int deviceType) {
        LogUtil.info(TAG, "setDevice start>>>");
        LogUtil.info(TAG, "setDevice dvbDeviceType=" + deviceType);
        dvbDeviceType = deviceType;
    }

    /**
     * 检测设备
     * @return 设备类型
     * @see #DVB_DEVICE_NONE
     * @see #DVB_DEVICE_ICAST
     * @see #DVB_DEVICE_ICAST_PLUS
     */
    public int detectDevice() {
        icastDevice.detectDevice();
        icastPlusDevice.detectDevice();
        return dvbDeviceType;
    }

    /**
     * 处理设备状态回调
     * @param msg 消息
     * @param param 参数
     */
    public void handleDeviceCallbackMsg(int msg, Object param) {
        if (dvbDevice == null) {
            LogUtil.info(TAG, "handleDeviceCallbackMsg dvbDevice is null");
            return;
        }
        List<IDVBDeviceCallback> callbackList = dvbDevice.getCallbackList();
        if (callbackList != null && callbackList.size() > 0) {
            for (IDVBDeviceCallback callback : callbackList) {
                callback.handleDVBDeviceCallbackMsg(msg, param);
            }
        }
    }
}